from numpy import log2,sum,array
def entropy(p):
    p = array(p)
    if 1 in p:
        return 0
    else:
        s = -(p * log2(p)).sum()
    return s


if __name__ == "__main__":
    s = entropy([5/14,9/15])
    # print(f'原始信息熵：{s:.4f} 对应概率 [5/14, 9/14]')
    # print(f'原始信息熵：{entropy([5/14,9/15])} 对应概率 [5/14, 9/14]')
    # print("原始信息熵：",entropy([5/14,9/15]))
    # 以outlook作为切分条件带来的条件熵   前1后0
    e_outlook_sunny = entropy([3/5,2/5])
    e_outlook_overcast = entropy([0/4,4/4])
    e_outlook_rainy = entropy([2/5,3/5])
    e_outlook = e_outlook_overcast * 5 / 14 + e_outlook_sunny * 4 / 14 + e_outlook_rainy * 5 / 14
    # outlook的信息增熵:
    dalta_outlook = entropy([5/14,9/14]) - e_outlook
    print(dalta_outlook)